package com.cuit.qx.service;


import com.cuit.qx.bean.PageList;
import com.cuit.qx.bean.RoleGroup;
import com.cuit.qx.dao.RoleGroupMapper;
import com.cuit.qx.utils.tools.StringUtils;
import com.github.pagehelper.PageHelper;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class RoleManagerService {
	static Logger logger = LogManager.getLogger(RoleManagerService.class.getName());
	
	@Autowired
	private RoleGroupMapper groupMapper;

	/**
	 * 查询用户的所有角色
	 * @return
	 */
	public List<RoleGroup> getRoleListByUser() {
		return groupMapper.selectRoleListAll();
	}

	/**
	 * 查询角色列表
	 * @param roleName
	 * @param roleStatus
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<RoleGroup> queryRolePageList(String searchKey, String roleName, String roleStatus, Integer page, Integer rows) {
		List<RoleGroup> list =  groupMapper.selectRoleList(searchKey,roleName,roleStatus,page,rows);
		return list;
	}

	/**
	 * 统计角色列表
	 * @param roleName
	 * @param roleStatus
	 * @return
	 */
	public int queryRolePageListCount(String searchKey, String roleName, String roleStatus) {
		int count =  groupMapper.selectRoleListCount(searchKey,roleName,roleStatus);
		return count;
	}

	/**
	 * 删除角色
	 * @param roleId
	 * @return
	 */
	public int deleteRole(String roleId) {
		return groupMapper.deleteByPrimaryKey(roleId);
	}

	/**
	 * 新增角色
	 * @param role
	 * @return
	 */
	public int addRole(RoleGroup role) {
		return groupMapper.insertSelective(role);
	}

	/**
	 * 查询是否有重复角色
	 * @param roleName
	 * @return
	 */
	public int checkRoleName(String roleName) {
		return groupMapper.checkRoleName(roleName);
	}

	/**
	 * 查询角色信息
	 * @param roleId
	 * @return
	 */
	public RoleGroup queryRoleBeanInfo(String roleId) {
		return groupMapper.selectRoleInfoById(roleId);
	}

	public int updateRole(RoleGroup role) {
	    return groupMapper.updateByPrimaryKeySelective(role);
	}

	/**
	 * 查询角色是否有菜单权限
	 * @param roleId
	 * @return
	 */
	public boolean isExsitsRoleMenu(String roleId) {
		return groupMapper.isExsitsRoleMenu(roleId) != 0;
	}

	/**
	 * 修改状态
	 * @param roleId
	 */
	public void updateRoleMenuStatusByRoleId(String roleId) {
		groupMapper.updateRoleMenuStatusByRoleId(1,roleId);
	}

	public void updateRoleMenuPermission(String roleId, String[] sourceId) {
		for (int i = 0; i < sourceId.length; i++)
		{
			String menuId = sourceId[i];
			int exsitCount = groupMapper.queryExsit(roleId,menuId);
			if (exsitCount != 0)
			{
				groupMapper.updateRoleMenu(0,roleId, menuId);
			}
			else
			{
				groupMapper.insertRoleMenu(StringUtils.getPrimarykeyId(),roleId, menuId, 0);
			}
		}
	}

	public void addRoleMenuList(String[] sourceId, String roleId) {
		List<Map<String,Object>> list = new LinkedList<>();
		for (int i = 0; i < sourceId.length; i++)
		{
			Map<String,Object> array = null;
			array = new HashMap<String, Object>();
			array.put("keyId", StringUtils.getPrimarykeyId());
			array.put("roleId",roleId);
			array.put("menuId",sourceId[i]);
			array.put("status",0);
//			if(!roleId.startsWith("100")){
//				 array.put("type",1);
//			}else{
//				 array.put("type",2);
//			}
			array.put("type",0);
			list.add(array);
		}
		groupMapper.insertRoleMenuList(list);
	}

	public int changeRoleStatus(String roleid, String status) {
		return groupMapper.updateRoleStatus(roleid,Integer.parseInt(status));
	}
	
}
